﻿using UnityEngine;
using System.Collections;
using System;

//All time in this class is in MS
public class TimeManager : Singleton<TimeManager> 
{	
	#region Fields 
	
	public MorningHandler OnMorning;
	
	#endregion
	
	#region Delegates

	public delegate void MorningHandler();
	
	#endregion
	
	private float m_runningClock;
	
	private const float cSmudgeFactor = 5000.0f;
	
	public float Hours
	{
		get { return (int)(m_runningClock / 3600.0f); }
	}
	
	public float Minutes
	{
		get { return (int)(m_runningClock / 60.0f) % 60; }
	}
	
	public float Seconds
	{
		get { return m_runningClock % 60; }
	}
	
	public bool Paused
	{
		set; get;
	}
	
	void Start () 
	{
		m_runningClock = 0;
		Paused = true;
	}

	void Update () 
	{
		UpdateClock();
	}
	
	//Private functions for the timer
	private void UpdateClock()
	{
		//Only update the clock if we are currently not paused
		if (!Paused)
		{
			//dt is in seconds
			float dt = (Time.deltaTime) * cSmudgeFactor;
			
			m_runningClock += dt;
			OnMorning();
			//Number of seconds in a day
			if (m_runningClock > 86400)
			{
				m_runningClock = 0;	
			}
		}
	}
}
